package com.example.demo.security;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {
    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User userByPhone = userMapper.getUserByPhone(username);
        if (Objects.isNull(userByPhone)) {
            throw new UsernameNotFoundException("用户不存在");
        }
        LoginUserDetails loginUserDetails = new LoginUserDetails(userByPhone);
        return loginUserDetails;
    }
}
